#include <iostream>
#include <map>
#include <vector>
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& v)
{
for (T& a : v)
input >> a;
return input;
}
void answer(int x)
{
std::cout << x << '\n';
}
void consume(std::map<int, unsigned>& f, int x, unsigned d)
{
const auto it = f.find(x);
it->second -= d;
if (it->second == 0)
f.erase(it);
}
void solve(const std::vector<int>& a, size_t k)
{
const size_t n = a.size();
std::map<int, unsigned> f;
for (const int x : a)
++f[x];
const auto check = [&](std::map<int, unsigned> s) {
int v = 0;
std::map<int, unsigned> t = f;
for (const auto& e : s) {
v += e.first * int(e.second);
consume(t, e.first, e.second);
}
for (unsigned q = 0; q < k && !s.empty() && !t.empty() && s.begin()->first < t.rbegin()->first; ++q) {
v -= s.begin()->first;
consume(s, s.begin()->first, 1);
v += t.rbegin()->first;
consume(t, t.rbegin()->first, 1);
}
return v;
};
int x = a[0];
for (size_t i = 0; i < n; ++i) {
std::map<int, unsigned> s;
for (size_t j = i; j < n; ++j) {
++s[a[j]];
x = std::max(x, check(s));
}
}
answer(x);
}
int main()
{
std::cin.tie(nullptr)->sync_with_stdio(false);
size_t n, k;
std::cin >> n >> k;
std::vector<int> a(n);
std::cin >> a;
solve(a, k);
return 0;
}
102. Binary Tree Level Order Traversal | 96. Unique Binary Search Trees |
75. Sort Colors | 74. Search a 2D Matrix |
71. Simplify Path | 62. Unique Paths |
50. Pow(x, n) | 43. Multiply Strings |
34. Find First and Last Position of Element in Sorted Array | 33. Search in Rotated Sorted Array |
17. Letter Combinations of a Phone Number | 5. Longest Palindromic Substring |
3. Longest Substring Without Repeating Characters | 1312. Minimum Insertion Steps to Make a String Palindrome |
1092. Shortest Common Supersequence | 1044. Longest Duplicate Substring |
1032. Stream of Characters | 987. Vertical Order Traversal of a Binary Tree |
952. Largest Component Size by Common Factor | 212. Word Search II |
174. Dungeon Game | 127. Word Ladder |
123. Best Time to Buy and Sell Stock III | 85. Maximal Rectangle |
84. Largest Rectangle in Histogram | 60. Permutation Sequence |
42. Trapping Rain Water | 32. Longest Valid Parentheses |
Cutting a material | Bubble Sort |